﻿#if UNITY_EDITOR
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class SkillEditerLayer : OdinMenuEditorWindow
{

    protected override OdinMenuTree BuildMenuTree()
    {
        var tree = new OdinMenuTree(true);
        tree.DefaultMenuStyle.IconSize = 28.00f;
        tree.Config.DrawSearchToolbar = true;
        
        SkillUnitEditerManager.InitMenu<SkillData>(tree);
        SkillUnitEditerManager.InitMenu<SkillBulletTriggerData>(tree);
        SkillUnitEditerManager.InitMenu<SkillBulletData>(tree);
        SkillUnitEditerManager.InitMenu<BulletShapeData>(tree);
        SkillUnitEditerManager.InitMenu<BulletSiteTargetData>(tree);
        SkillUnitEditerManager.InitMenu<BulletTrailData>(tree);
        SkillUnitEditerManager.InitMenu<BulletTriggerData>(tree);
        SkillUnitEditerManager.InitMenu<BulletFinishData>(tree);
        
        tree.MenuItems.ForEach(AddDragHandles);
        tree.EnumerateTree().Where(x => x.Value as MyScriptableObject).ForEach(AddDragHandles);
        return tree;
    }

    private void AddDragHandles(OdinMenuItem menuItem)
    {
        menuItem.OnDrawItem += x => DragAndDropUtilities.DragZone(menuItem.Rect, menuItem.Value, false, false);
    }


    protected override void OnBeginDrawEditors()
    {

        
        var selected = this.MenuTree.Selection.FirstOrDefault();
        var toolbarHeight = this.MenuTree.Config.SearchToolbarHeight;
        
        SirenixEditorGUI.BeginHorizontalToolbar(toolbarHeight);
        {
            if (selected != null)
            {
                GUILayout.Label(selected.Name);
            }
            

            if (SirenixEditorGUI.ToolbarButton(new GUIContent("(+)")))
            {
                ScriptableObjectCreator.ShowDialog<MyScriptableObject>(MyAssetsConfig.skillAssetsPath,  obj =>
                {
                    obj.Name = obj.name;
                    base.TrySelectMenuItemWithObject(obj); // Selects the newly created item in the editor
                });
            }

        }
        SirenixEditorGUI.EndHorizontalToolbar();
    }
    
}
#endif